package simulation;

import gnu.cajo.utils.extra.TransparentItemProxy;

import combat.protocol.interfaces.SimulationInt;
import combat.protocol.interfaces.TeamInt;
import combat.protocol.interfaces.UserInt;
import combat.protocol.interfaces.VehicleInt;

public class Connection
{
	public static SimulationInt simulation;
	private static UserInt me;
	private static TeamInt myTeam;
	private static VehicleInt myBoat;
	
    public static boolean init()
    {
        try 
        {
        	// Esatablish connection to server
            simulation = (SimulationInt) TransparentItemProxy.getItem(
            		"//" + Config.cajoHost + ":" + Config.cajoPort + "/Simulation", new Class[] { SimulationInt.class });
            
            // Call update function in Data.java
            Data.Update();
            
            // Return true if connection and update functions succeeded 
            return true;
        }
        catch (Exception e) 
        {
        	System.out.println("ERROR: Could not connect to CMS Server.");
        	
        	// Return false if connection or update function is not succesful
			return false;
        }
    }
    
    public static void login(String username)
    {
    	// Log me in and fill the 'me' variable 
    	Object meRemote = simulation.userLogOn_Remote(username);
		me = (UserInt) TransparentItemProxy.getItem(meRemote, new Class[] { UserInt.class });

		System.out.println(getMe().getUserName() + " has logged in.");
    }
    
    // Get my team
	public static TeamInt getMyTeam() 
	{
		
		Object myTeamRemote = simulation.findTeamOfUser_Remote(getMe());
		myTeam = (TeamInt) TransparentItemProxy.getItem(myTeamRemote, new Class[] { TeamInt.class });
		
		// Return my team
		return myTeam;
	}

	// Get me
	public static UserInt getMe() 
	{
		return me;
	}
	
	// Get my vehicle
	public static VehicleInt getMyVehicle()
	{
		Object myBoatRemote = Connection.getMe().getUserVehicle_Remote();
		myBoat = (VehicleInt) TransparentItemProxy.getItem(myBoatRemote, new Class[] { VehicleInt.class });
		
		// Return my vehicle
		return myBoat;
	}
}
